/*
 * iSGL3D: http://isgl3d.com
 *
 * Copyright (c) 2010-2011 Stuart Caunt
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#import "isgl3d.h"
#import "Isgl3dPhysicsWorld.h"

#include "btHeightfieldTerrainShape.h"

@class Isgl3dDemoCameraController;
@class Isgl3dPhysicsWorld;

//=======================================================
// Physics
//=======================================================
class btDefaultCollisionConfiguration;
class btDbvtBroadphase;
class btCollisionDispatcher;
class btSequentialImpulseConstraintSolver;
class btDiscreteDynamicsWorld;

//=======================================================
// World generate
//=======================================================
@interface RSWorld : Isgl3dPhysicsWorld {
@public
    float   chunkWidth;                     //some config data
    float   cellUnitWidth;                      
    int     cellsPerChunk;
    
    float   viewRadius;
    
    
    NSMutableDictionary* currentChunks;     // a looking table contains all exist chunks.
    
    
    btDefaultCollisionConfiguration * _collisionConfig;
	btDbvtBroadphase * _broadphase;
	btCollisionDispatcher * _collisionDispatcher;
	btSequentialImpulseConstraintSolver * _constraintSolver;
}
- (id) init;
- (bool) isChunkExist:(NSString*)key;
- (void) refreshView:(Isgl3dNode*)centerTarget;
- (Isgl3dPhysicsObject3D *) createPhysicsObject:(Isgl3dMeshNode *)node shape:(btCollisionShape *)shape mass:(float)mass restitution:(float)restitution isFalling:(BOOL)isFalling;
@end


@interface RSWorldChunk : Isgl3dNode {
@public
    
    float waterAltitude;

    RSWorld* parent;                            // RSWorld that hold this chunk;
    
    Isgl3dTerrainMeshRandom*    terrainMesh;    // Terrain Mesh object, This is just a Mesh object, cannot be rendered.
    btHeightfieldTerrainShape*  terrainShape;   // Terrain Physics Shape object;
    Isgl3dPhysicsObject3D*      terrainPhysics; // Terrain Physics obj
    
    Isgl3dMeshNode* terrain;                // This is the terrain container object that can be rendered;
    Isgl3dNode* water;                     // Water plane  
    Isgl3dNode* objects;                    // Other objects on this ground.
    
}
- (id)   init:(RSWorld*)p worldx:(float)x worldz:(float)z;
- (void) generateObjects;
@end

//=======================================================
// Application
//=======================================================
@interface Isgl3dTutorial1View : Isgl3dBasic3DView {

@public
    NSMutableArray* cubes;
    
    Isgl3dDemoCameraController * _cameraController;
    Isgl3dAction * action;
    
    NSMutableArray* terrainChunks;
    
    Isgl3dNode* snail;
    Isgl3dNode* sky;
    
    Isgl3dPhysicsObject3D* snailPhysics;
    
    Isgl3dShadowCastingLight * light;
    
    Isgl3dFollowCamera* _followCamera;
    
    Isgl3dVector3 followCameraPosition;
    Isgl3dVector3 followCameraLookAt;
    
    
    RSWorld* world;
}

@end
//=======================================================
// User Interface
//=======================================================
@interface SnailRushUIView : Isgl3dBasic2DView {
@private
    Isgl3dTutorial1View* _snailRush;
}

@property (nonatomic) Isgl3dTutorial1View* snailRush;
@end

//=======================================================
// Shaders
//=======================================================
@interface TerrainShader : Isgl3dCustomShader {
	Isgl3dGLTexture * _materialTexture;
	Isgl3dGLTexture * _bumpTexture;
	
	float _fAnim;
}

+ (id) shaderWithKey:(NSString *)key;
- (id) initWithKey:(NSString *)key;

@end

//-------------------------------------------------------
@interface ToonShader : Isgl3dCustomShader {
@private
	Isgl3dGLTexture * _materialTexture;
    Isgl3dGLTexture * _bumpTexture;
}

+ (id) shaderWithKey:(NSString *)key;
- (id) initWithKey:(NSString *)key;

@end
//-------------------------------------------------------
@interface FlatShader : Isgl3dCustomShader {
@public
	Isgl3dGLTexture* materialTexture;
    float waveOffset;
}

+ (id) shaderWithKey:(NSString *)key;
- (id) initWithKey:(NSString *)key;

@end

/*
 * Principal class to be instantiated in main.h. 
 */
#import "Isgl3dAppDelegate.h"
@interface AppDelegate : Isgl3dAppDelegate
- (void) createViews;
@end
